﻿#include "MyInitial.h"

void Logger::init()
{
    // create console and file logger
    auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_st>();
    // use relative path and ensure file is accessible
    std::shared_ptr<spdlog::sinks::basic_file_sink_mt> file_sink;
    try
    {
        file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/cuda_test.log", true);
    }
    catch (const std::exception &e)
    {
        // if file creation fails, use console output only
        spdlog::warn("Failed to create file sink: {}", e.what());
        auto logger = std::make_shared<spdlog::logger>("cuda_test", spdlog::sinks_init_list{console_sink});
        logger->set_level(spdlog::level::debug);
        spdlog::set_default_logger(logger);
        return;
    }

    //======================= console sink
    console_sink->set_level(spdlog::level::info);
    console_sink->set_pattern("%^[%T] %v%$");  // 控制台带颜色格式
    //console_sink->set_pattern("%^[%L] %v%$");  // 控制台带颜色格式
    //console_sink->set_pattern("%^%v%$");  // 控制台带颜色格式
    //======================= console sink
    file_sink->set_level(spdlog::level::debug);
    file_sink->set_pattern("%^[%T] %v%$");  // 带颜色格式
    //file_sink->set_pattern("[%L%T] %v");  // 文件无颜色格式
    //file_sink->set_pattern("[%T] %v");  // 文件无颜色格式

    //======================= create multi-sink logger
    auto logger = std::make_shared<spdlog::logger>("cuda_test", spdlog::sinks_init_list{console_sink, file_sink});

    // set as default logger
    spdlog::set_default_logger(logger);

    // 命令行切换为utf-8编码
#ifdef _WIN32
    system("chcp 65001 > nul");
#endif
}
std::shared_ptr<spdlog::logger> Logger::getLogger() { return spdlog::get("cuda_test"); }
